package com.qf;

import com.qf.mapper.UserMapper;
import com.qf.pojo.TbUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author langazhe
 * @version V1.0
 * @Project mavens
 * @Package com.qf
 * @Description:
 * @Date 2022/5/17 23:11
 */
public class testCache {
    @Test
    public void testCache() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取到了一个数据库的连接
        SqlSession sqlSession = sessionFactory.openSession();
        //获取到mapper,用于执行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.err.println("第一次查询");
        TbUser user = userMapper.findById(3L);
        System.err.println(user);
        System.err.println("第二次查询");
        user = userMapper.findById(3L);
        System.err.println(user);
        sqlSession.close();//关闭会话
        System.err.println("第三次查询");
        SqlSession sqlSession2 = sessionFactory.openSession();
        UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
        TbUser user1 = userMapper2.findById(3L);
        System.err.println(user1);

    }
}
